#ifndef BOARDREGISTER_H
#define BOARDREGISTER_H

#include "canframe.h"
#include <QThread>
#include <QtGui/QDialog>
class QLabel;
class QPushButton;
class QTimer;
class QDataStream;
#define LOOPMAXINUM 4
#define BOARDMAXINUM 35
#define LOUXIANNUM 33
#define ZHIKONGNUM 34
#define ZONGXIANNUM 35
#define NODEMAXINUM  256
#define LOUXIANMAXNUM 250
#define ZHIKONGMAXNUM 20
#define ZONGXIANMAXNUM 64

#define OFFLINE 0
#define ONLINE 1
#define FAULT 2
#define OTHER 3

#define LOUXIAN  90
#define ZHIKONG  91
#define ZONGXIAN 92

class boardRegThread : public QThread
{
    Q_OBJECT
public:
    boardRegThread(QObject *parent = 0);
    ~boardRegThread();

private:
    int i;//current board number
    int can_fd;
    int nwrite;
    bool boardRegFlag[BOARDMAXINUM];//reg,1;not reg,0
    int boardRegSum;//total number of registed board
    int boardState;//board state,ONLINE,OFFLINE,FAULT,OTHER
    int loopRegNum[4];//4 Bytes ,registerd node number of 4 loops of  a board
    int louxianRegNum;//registed louxian number
    int zhikongRegNum;//registed zhikongpan number
    int zongxianRegNum;//registed zongxianpan number
    unsigned char nodeType[LOOPMAXINUM][NODEMAXINUM];//[4][256]
    unsigned char louxianReg[LOUXIANMAXNUM];//[250],state:online or offline
    unsigned char zhikongReg[ZHIKONGMAXNUM];
    unsigned char zongxianReg[ZONGXIANMAXNUM];
    struct can_frame sendFrame,recvFrame;
    QTimer *timer1;

    bool isTimeOut;
    void sendBroadcast();
    void sendSingle();
    void sendAck();
    void dealInfo(int );
    //void saveFlagFile();
    void verifyData();
    void saveNodeToDB(int boardnum, unsigned char nodeType[][NODEMAXINUM]);
    void saveBoardRegFlagToDB(bool boardRegFlag[BOARDMAXINUM]);//

protected:
    void run();
private slots:
    void timeOutSlot();
signals:
    void regStart();
    void regFault();
    void regAllOver(int );
};
class BoardRegDialog : public QDialog
{
    Q_OBJECT

public:
    BoardRegDialog(QWidget *parent = 0);
    ~BoardRegDialog();

private:
    boardRegThread *boardthread;
    QLabel *infoLabel;
    QPushButton *okPushButton;
    QPushButton *cancelPushButton;


private slots:
    void regStartMessage();
    void regFaultMessage();
    void regAllOverMessage(int );
signals:
    void startPolling();
};

#endif // BOARDREGISTER_H
